Day 11 - Find and replace text
– I’ve got to find a replacement for the chief.
Das Boot (1981)
Searching is one of the more frequent activities of computers, algorithms, and also programmers.
You search a given variable in a text, you want to find all the occurrences of a name in a book, you
want to extract all the timestamps in a log file with a specific day and hour. These are just some
examples of how searching can be used in your daily life (as a programmer or not).
Replacing text is less frequent than searching only because you fist need to search for the text in
order to replace it. You want to replace text because you need to format some data in a different
way, because you want to reuse part of it, and in general because you want to transform data.
In this chapter I will introduce two tools that I use every single day, and definitely in every script
that I write, grep and sed. We already briefly covered sed in chapter 6, but in this chapter we will
start discovering its true potential. Both tools, sed in particular, are very powerful and provide many
options, thus I will not cover them completely in this book. You will find a lot of information in the
respective man pages and in plenty of online resources covering these tools.
We will unlock the full power of both grep and sed only once we will learn regular expressions in
Part 2 of the book, but in this chapter we can start familiarising with the syntax and with their role.
In the whole chapter I will make use of the examplex.txt file that you should have among the files
provided for the book.
Let’s start with grep. This tool was born inside the grandfather of all editors, ed, as the “Globally
search a Regular Expression and Print” command, and was eventually converted into the stand-
alone utility that we are still using nowadays. The tool’s man page says that grep prints lines that
match patterns, thus it’s the tool that we will use every time we need to find specific parts of some
text.
Are you already asleep? I am dozing. Enough chatting! Code first and boring explanations later!
If you execute this code
$ grep "elephant" examples.txt
elephant
you notice that grep found the line that contained the proovided string elephant and printed it. The
tool find partially matching lines by default, that is lines that contain the searched string and other
characters.